//
//  WBbaseView.swift
//  微博
//
//  Created by lipengfei on 15/9/3.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

class WBbaseView: UIView {
    
    ///初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    /// 允许使用 xib
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
///  修改view中的属性
    func setAttr(imageName:String?,textMess:String){
//        通过修改iconView 的图片，和文字，显示不同的内容
//        主页有动画，通过判断是否传递了图片名称判断是否为主页面，主页面开启动画
        if let img = imageName{
            // 有图片，进入代码块执行
            iconView.image = UIImage(named: img)
                        // 非主页面，隐藏小房子
            houseView.hidden = true
            //由于蒙版在iconView的上面，所以需要将蒙版放到View的最下面
            sendSubviewToBack(maskImgView)
        }
        else
        {
            startAnimation()
        }
        textView.text = textMess
    }
    
    ///设置动画效果
    private func startAnimation(){
//        创建一个动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    ///将控件添加到view中并设置约束
    private func setupView(){
        addSubview(iconView)
        addSubview(maskImgView)
        addSubview(houseView)
        addSubview(textView)
        addSubview(loginView)
        addSubview(registerView)
        
        
        //添加约束
    /// iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -20))
 ///house
        houseView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
 /// text
        textView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[text(224)]", options: [], metrics: nil, views: ["text":textView]))
        addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
 /// register
        // 4> register
        registerView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[register(==100)]", options: [], metrics: nil, views: ["register":registerView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[register(==35)]", options: [], metrics: nil, views: ["register":registerView]))
        // 4> register
        loginView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[login(==100)]", options: [], metrics: nil, views: ["login":loginView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[login(==35)]", options: [], metrics: nil, views: ["login":loginView]))
 /// 添加蒙版
        maskImgView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskImgView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[mask]-(-35)-[login]", options: [], metrics: nil, views: ["mask":maskImgView,"login":loginView]))
    }
///  设置view中的各种控件
///  iconView
    private var iconView:UIImageView = {
        let icon = UIImageView()
        icon.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return icon
    }()
///  houseView
    private var houseView:UIImageView = {
        let house = UIImageView()
        house.image = UIImage(named: "visitordiscover_feed_image_house")
        return house
    }()
///  textView
    private var textView:UILabel = {
        let textLabel = UILabel()
      //  textLabel.text = "哈啊哈哈哈哈哈哈啊哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈啊哈哈哈哈哈啊哈哈哈哈哈哈哈"
        textLabel.textAlignment = NSTextAlignment.Center
        textLabel.textColor = UIColor.darkGrayColor()
        textLabel.font = UIFont.systemFontOfSize(14)
        textLabel.numberOfLines = 0
        return textLabel
    }()
///  loginView
     var loginView:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
        }()
    ///  registerView
     var registerView:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
        }()
///     maskImgView
    private var maskImgView:UIImageView = {
        let mask = UIImageView()
        mask.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return mask
    }()
    
}
